package ca.compsci.opent.doctool;

import java.util.Arrays;
import java.util.List;

/**
 * Models a procedure in OpenT.
 * 
 * @since 0.0.2
 */
public class Procedure
{
	/**
	 * Procedure's identifier in OpenT code.
	 * 
	 * @since 0.0.2
	 */
	private String identifier;
	
	/**
	 * List of procedures parameters.
	 * 
	 * @since 0.0.2
	 */
	private List<Param> params;

    /**
     * Create a new procedure with identifier and parameters.
	 * 
	 * @param identifier name of this procedure in OpenT code
	 * @param params paramters ot this procedure
	 * 
	 * @since 0.0.2
     */
    public Procedure(String identifier, Param... params)
    {
		this.identifier = identifier;
		this.params = Arrays.asList(params);
    }
	
	/**
	 * Returns this procedures identifier.
	 * 
	 * @return procedure's identifier
	 * 
	 * @since 0.0.2
	 */
	public String getIdentifier()
	{
		return identifier;
	}
	
	/**
	 * Returns this procedures list of parameters.
	 * 
	 * @return list of this procedure's paramaters
	 * 
	 * @since 0.0.2
	 */
	public List<Param> getParams()
	{
		return params;
	}
	
	/**
	 * Gets the parameter at the specified index.
	 * 
	 * @param i index
	 * 
	 * @return parameter at index {@code i}
	 * 
	 * @since 0.0.2
	 */
	public Param getParam(int i)
	{
		return params.get(i);
	}
	
	/**
	 * Returns the string representation of this procedure.
	 * 
	 * <p>
	 * The representation includes the procedure's identifier and the list of
	 * its paramaters:
	 * </p>
	 * 
	 * <p>
	 * {@code procedure <i>identifier</i> [(<i>param_identifier</i> : <i>type</i>
	 * {, <i>param_identifier</i>} : <i>type</i>)]}
	 * </p>
	 * 
	 * @return string representation of this procedure
	 * 
	 * @since 0.0.2
	 */
	@Override
	public String toString()
	{
		StringBuilder s = new StringBuilder();
		
		s.append("procedure ").append(identifier);
		
		if (!params.isEmpty())
		{
			s.append("(");
			s.append(params.get(0));
			for (int i = 1; i < params.size(); i++)
			{
				s.append(params.get(i));
			}
			s.append(")");
		}
		
		return new String(s);
	}
}
